using System;
using System.Collections.Generic;
using System.Text;
using LngT.Interfaces;
using LngT.Interfaces.DBUserRoles;
using System.Collections.ObjectModel;

namespace LngT.Core
    {
    [Serializable]
    class Controller: MarshalByRefObject, IController
        {

        public Controller( IDatabase database )
            {
            _database = database;
            }

        private IDBUserRole _permissions = new DBUSerRoleDefault( null );
        private bool _authenticated = false;
        private string _userName = null;
        private string _password = null;

        public bool Authenticate( string userName, string password )
            {
            IDBUserRole newRole = _database.GetDBUserPrivileges( userName, password );
            if( newRole != null )
                {
                _permissions = newRole;
                _userName = userName;
                _password = password;
                _authenticated = true;
                return true;
                }
            _authenticated = false;
            return false;
            }

        public int AddItem( IItem item )
            {
            if( !_permissions.CanAddItem() )
                throw new AuthenticationException();

            return _database.AddItem( item );
            }

        public bool RemoveItem( int itemId )
            {
            if( !_permissions.CanRemoveItem() )
                throw new AuthenticationException();

            return _database.RemoveItem( itemId );
            }

        public bool UpdateItem( int itemId, IItem item )
            {
            if( !_permissions.CanUpdateItem() )
                throw new AuthenticationException();

            return _database.UpdateItem( itemId, item );
            }

        private IDatabase _database;
        internal IDatabase Database
            {
            get
                {
                return _database;
                }
            set
                {
                _database = value;
                }
            }

        public string Name
            {
            get
                {
                return _database.Name;
                }
            set
                {
                _database.Name = value;
                }
            }

        public IItem GetItem( int itemId )
            {
            if( !_permissions.CanGetItem() )
                throw new AuthenticationException();

            return _database.GetItem( itemId );
            }

        public void AddDBUser( string userName, string password, IDBUserRole privileges )
            {
            if( !_permissions.CanAddUsers() )
                throw new AuthenticationException();

            _database.AddDBUser( userName, password, privileges );
            }

        public bool RemoveDBUser( string userName )
            {
            if( !_permissions.CanRemoveUsers() )
                throw new AuthenticationException();

            return _database.RemoveDBUser( userName );
            }

        public bool SetDBUserPrivileges( string userName, IDBUserRole privileges )
            {
            if( !_permissions.CanChangeUserPrivileges() )
                throw new AuthenticationException();

            return _database.SetDBUserPrivileges( userName, privileges );
            }


        public IEnumerable<IItem> GetItemIterator( IIterationAlgorithm algorithm )
            {
            if( !_permissions.CanIterate() )
                throw new AuthenticationException();

            return _database.GetItemIterator( algorithm );
            }

        public int GetCurrentUserID()
            {
            if( !_authenticated )
                throw new AuthenticationException();
            return _database.GetUserID( _userName, _password );
            }
        }
    }
